﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Yugi.Client.Control
{
    public class HostItem
    {
        public static Canvas Presenter;
        public static int DELAY=500;

        public Border Host { get; set; }
        public YCard CardControl { get; set; }
        public bool IsFree { get; set; }

        public HostItem(YCard card,double x,double y)
        {
            CardControl = card;
            //Init Data
            Host = new Border();
            Canvas.SetLeft(card,0);
            Canvas.SetTop(card, 0);
            Presenter.Children.Add(card);
            Host.Margin = card.Margin;
            Host.Width = card.Width;
            Host.Height = card.Height;
            //Assign value
            IsFree = false;
            
            InitTransform(x,y);
            InitTimer();
            Host.LayoutUpdated += new EventHandler(Host_LayoutUpdated);
        }
        void InitTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(DELAY+200);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void InitTransform(double x,double y)
        {
            TransformGroup group = new TransformGroup();
            translate = new TranslateTransform(x,y);
            rotate = new RotateTransform();
            scale = new ScaleTransform();
            group.Children.Add(translate);
            group.Children.Add(rotate);
            group.Children.Add(scale);
            CardControl.RenderTransform = group;

        }
        public void SetPosition(double x,double y)
        {
            Grid.SetColumn(this.Host, 0);
            Grid.SetRow(this.Host, 0);
            this.Host.Margin = new Thickness(x, y, 0, 0);
        }
        public void Host_LayoutUpdated(object sender, EventArgs e)
        {
            if (IsFree || is_animating) return;
            // Remove CardControl
            if (Host.Parent == null)
            {
                Presenter.Children.Remove(CardControl);
                Host.LayoutUpdated -= new EventHandler(Host_LayoutUpdated);
                return;
            }
            Point toa_do = Host.TranslatePoint(new Point(0, 0), HostItem.Presenter);
            double dx= Math.Round(translate.X - toa_do.X,4);
            double dy=Math.Round(translate.Y - toa_do.Y,4);
            if ( dx != 0 || dy != 0)
            {
                translate.BeginAnimation(TranslateTransform.XProperty, SetupDoubleAnimation(translate.X, toa_do.X, DELAY));

                translate.BeginAnimation(TranslateTransform.YProperty, SetupDoubleAnimation(translate.Y, toa_do.Y, DELAY));
                is_animating = true;

                timer.Start();
                System.Diagnostics.Debug.WriteLine(dem++ + " : " +dx);
            }
            
        }
        static int dem=0;
        public DoubleAnimation SetupDoubleAnimation(double From, double To, int time)
        {
            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = From;
            myDoubleAnimation.To = To;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(time));
            myDoubleAnimation.AutoReverse = false;

            return myDoubleAnimation;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            double xBuffer = translate.X;
            double yBuffer = translate.Y;

            translate.BeginAnimation(TranslateTransform.XProperty, null);
            translate.BeginAnimation(TranslateTransform.YProperty, null);

            translate.X = xBuffer;
            translate.Y = yBuffer;
            is_animating=false;
            timer.Stop();
        }
        bool is_animating;
        public Point Position
        {
            get
            {
                return new Point(translate.X, translate.Y);
            }
        }
        TranslateTransform translate;
        RotateTransform    rotate;
        ScaleTransform     scale;
        DispatcherTimer    timer;
    }
}
